package com.linln.admin.miss.service.impl;

import com.linln.admin.miss.domain.MineSpot;
import com.linln.admin.miss.domain.SpotPool;
import com.linln.admin.miss.repository.MineSpotRepository;
import com.linln.admin.miss.repository.SpotPoolRepository;
import com.linln.admin.miss.service.MineSpotService;
import com.linln.admin.miss.service.SpotPoolService;
import com.linln.common.data.PageSort;
import com.linln.common.enums.StatusEnum;
import com.linln.common.utils.ResultVoUtil;
import com.linln.common.vo.ResultVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * @author hyf
 * @date 2021/10/11
 */
@Service
public class SpotPoolServiceImpl implements SpotPoolService {

    @Autowired
    private SpotPoolRepository spotPoolRepository;

    @Autowired
    private MineSpotRepository mineSpotRepository;
    /**
     * 根据ID查询数据
     * @param id 主键ID
     */
    @Override
    public SpotPool getById(Long id) {
        return spotPoolRepository.findById(id).orElse(null);
    }

    /**
     * 获取分页列表数据
     * @param example 查询实例
     * @return 返回分页数据
     */
    @Override
    public Page<SpotPool> getPageList(Example<SpotPool> example) {
        // 创建分页对象
        PageRequest page = PageSort.pageRequest();
        return spotPoolRepository.findAll(example, page);
    }

    /**
     * 保存数据
     * @param spotPool 实体对象
     */
    @Override
    public SpotPool save(SpotPool spotPool) {
        return spotPoolRepository.save(spotPool);
    }

    /**
     * 状态(启用，冻结，删除)/批量状态处理
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean updateStatus(StatusEnum statusEnum, List<Long> idList) {
        return spotPoolRepository.updateStatus(statusEnum.getCode(), idList) > 0;
    }

    @Override
    public ResultVo getSpot(Long userId, String sex) {
        List<MineSpot> mineSpotList = mineSpotRepository.findAllByUserId(userId);
        List<Long> spotPoolList = new ArrayList<>();
        mineSpotList.forEach(mineSpot -> {
            spotPoolList.add(mineSpot.getId());
        });
        if (mineSpotList!=null&&mineSpotList.size()>0){
            SpotPool spotPool =  spotPoolRepository.findSpotPoolByIdIsNotInAndSexCode(spotPoolList,sex);
            if (spotPool==null){
                return ResultVoUtil.error("纸条被取完了");
            }
            return ResultVoUtil.success(spotPool);

        }else {
            SpotPool spotPool =  spotPoolRepository.findSpotPoolBySex(sex);
            return ResultVoUtil.success(spotPool);

        }
    }

    @Override
    public ResultVo saveSpot(SpotPool spotPool) {
        SpotPool spotPool1 = spotPoolRepository.findSpotPoolByWechatNumber(spotPool.getWechatNumber());
        if (spotPool1==null){
            spotPoolRepository.save(spotPool);
            return ResultVoUtil.SAVE_SUCCESS;
        }else {
            return ResultVoUtil.error("已存入");
        }
    }
}